//
//  ViewController.swift
//  test
//
//  Created by ronie_z on 2021/10/12.
//

import UIKit
import GCDWebServer


class ViewController: UIViewController, GCDWebServerDelegate {
    var webServer:GCDWebServer!
    var player:VTSimplePlayer!
    var dataView:UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 0.6)
        self.setUI()
    }

    func setUI() {
        dataView = UIView()
        dataView.backgroundColor = .clear
        self.view.addSubview(dataView)
        let scale = (UIImage(named: "20")?.size.width ?? 16)/(UIImage(named: "20")?.size.height ?? 16)
        dataView.frame = CGRect(x: 0, y: (self.view.bounds.height-self.view.bounds.width/scale)/2.0, width: self.view.bounds.width, height: self.view.bounds.width/scale)
        
        let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        btn.setTitle("生成", for: .normal)
        btn.backgroundColor = .red
        self.view.addSubview(btn)
        btn.addTarget(self, action: #selector(click), for: .touchUpInside)
        
        
//        let img = UIImage(data: try! UIImage(named: "12")!.jpegData(compressionQuality: 1.0))!
        VTMP4Encoder.image(toMP4: UIImage(named: "24")) { data in
            print(data?.count)
        }
    }
    
    deinit {
        print("delete current vc =======")
    }
    
    
    func setData() {
        webServer = GCDWebServer()
        let dir = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("tmp/www/").path
        do {
            try FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true, attributes: nil)
        } catch {
        }

        let documentsDirectory = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("tmp").path

        let path = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("test_output.mp4").path
        let toPath = URL(fileURLWithPath: dir).appendingPathComponent("text").path
        if FileManager.default.fileExists(atPath: toPath) {
            do {
                try FileManager.default.removeItem(atPath: toPath)
            } catch {
            }
        }
        do {
            try FileManager.default.copyItem(atPath: path, toPath: toPath)
            } catch {
        }
        //启动服务
        webServer.addGETHandler(forBasePath: "/", directoryPath: dir, indexFilename: nil, cacheAge: 3600, allowRangeRequests: true)
        webServer.start(withPort: 8989, bonjourName: nil)
        //  创建视频播放器，并播放加密的视频
        player = VTSimplePlayer()
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.25 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: { [self] in
            self.player?.playURL("jedi://text.m3u8", in: self.dataView)
        })
        
    }
    
    @objc func click(){
        self.setData()
    }
    
    
    func touch (){
        self.setData()
    }
}

